package com.songcan.oauth.confinguration;

import com.songcan.oauth.handle.CoreLoginSuccessHandler;
import com.songcan.oauth.other.CoreAuthenticationFilter;
import com.songcan.oauth.provider.CoreAuthenticationProvider;
import com.songcan.oauth.service.YfkUserService;
import com.songcan.oauth.service.YfkUserWxInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

import static com.songcan.common.contans.CoreContants.THRID_LOGIN;


@Component
@Slf4j
@Order(SecurityProperties.BASIC_AUTH_ORDER - 2)
public class SecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {





    private RedisTemplate redisTemplate;
    @Value("${weixin.public.app-id}")
    private String wxPublicAppId;
    @Value("${weixin.public.secret-key}")
    private String wxPublicSecretKey;

    private YfkUserService yfkUserService;
    private YfkUserWxInfoService yfkUserWxInfoService;
    private CoreLoginSuccessHandler coreLoginSuccessHandler;




    @Autowired
    public SecurityConfigurer(RedisTemplate redisTemplate,YfkUserService yfkUserService,YfkUserWxInfoService yfkUserWxInfoService,CoreLoginSuccessHandler coreLoginSuccessHandler ){

        this.yfkUserService = yfkUserService;
        this.yfkUserWxInfoService = yfkUserWxInfoService;
        this.coreLoginSuccessHandler = coreLoginSuccessHandler;
        this.redisTemplate = redisTemplate;
    }


    @Override
    public void configure(HttpSecurity http) throws Exception {

        CoreAuthenticationFilter coreAuthenticationFilter = new CoreAuthenticationFilter(wxPublicSecretKey,wxPublicAppId,yfkUserWxInfoService,yfkUserService);
        coreAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        coreAuthenticationFilter.setAuthenticationSuccessHandler(coreLoginSuccessHandler);
        coreAuthenticationFilter.setRedisTemplate(redisTemplate);

        CoreAuthenticationProvider coreAuthenticationProvider = new CoreAuthenticationProvider();
        http.antMatcher(THRID_LOGIN).authorizeRequests()
                .and()
                .addFilterAfter(coreAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .authenticationProvider(coreAuthenticationProvider);

        http.antMatcher(THRID_LOGIN).authorizeRequests();
    }








}
